package com.sherlocky.learning.java11.var;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * JEP 286 局部变量类型推断
 *
 * @author: zhangcx
 * @date: 2019/9/26 14:37
 */
public class VarSample {
    public static void main(String[] args) {
        // 自动推断 ArrayList<String>
        var list = new ArrayList<String>();
        // 自动推断 Stream<String>
        var stream = list.stream();

        var newList = List.of("hello", "biezhi");
        System.out.println(newList.getClass().getName());
        newList.forEach(System.out::println);

        var newList2 = List.of("hello", "biezhi", 123L);
        System.out.println(newList2.getClass().getName());
        newList2.forEach(System.out::println);

        String fileName = "./README.md";

        var path = Paths.get(fileName);
        try {
            var bytes = Files.readAllBytes(path);
            System.out.println("字节数组: " + bytes);
        } catch (IOException e) {
            //
        }

        try (var foo = new FileInputStream(new File("./README.md"))) {
            System.out.println(foo);
        } catch (Exception e) {
            // ignore
        }
        // 还可以用在 lambda 表达式中
    }
}
